package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()

	router.Use(countResponseTime)
	router.POST("/ping", func(c *gin.Context) {
		if v, exists := c.Get("example"); exists {
			fmt.Printf("example: %v \n", v)
		}

		c.JSON(http.StatusOK, gin.H{
			"msg": "pong",
		})
	})

	_ = router.Run()
}

func countResponseTime(c *gin.Context) {
	start := time.Now()
	c.Set("example", "123455")
	c.Next()
	// c.Abort()	// 让代码停止往后面执行
	fmt.Println("time: ", time.Since(start).Seconds())
	status := c.Writer.Status()
	fmt.Println("状态码: ", status)
}
